package com.imti.mall.user.controller;

import com.imti.mall.common.Api.CommonResult;
import com.imti.mall.common.service.RedisService;
import com.imti.mall.common.utils.JwtTokenUtil;
import com.imti.mall.model.user.pojo.UmsMember;

import com.imti.mall.user.service.UserMemberService;
import com.imti.mall.user.uitl.RandomUtil;
import com.imti.mall.user.uitl.Sample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/v1/user")
@Api( tags = "发送验证码、用户注册、登录、修改密码接口")
public class UserMemberController {

    @Autowired
    UserMemberService userMemberService;
    @Autowired
    RedisService redisService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    /**
     * 发送验证码，1、保存redis数据库 2、发送到手机 3、设置验证码过期时间
     *
     * @param phone
     * @return json
     */
    @ApiOperation(value = "发送验证码")
    @PostMapping ("/sendValideCode")
    public  CommonResult sendValideCode(String phone) throws Exception {

        String code = String.valueOf(RandomUtil.random());//生成随机数
        redisService.set(phone, code);
        Sample.shortMessage(phone, code);//发送手机验证码
        if (redisService.get(phone) != null) {
            System.out.println(code);
            redisService.expire(phone,  60 * 5);
            return CommonResult.success("验证码发送成功");
        } else {
            return CommonResult.failed("验证码发送失败");
        }

    }

    /**
     * 注册
     * @param umsMember
     * @return
     */
    @ApiOperation(value = "用户注册")
    @PostMapping ("reg")
    public CommonResult reg(UmsMember umsMember, Integer code) throws Exception {

        //检验手机号是否使用过
        Integer fPhone = userMemberService.findPhone(umsMember.getPhone());

        if (fPhone > 0) {//手机号已被使用过
            return CommonResult.success("手机号码已被注册");
        } else {
            String rediscode= (String) redisService.get(umsMember.getPhone());
            System.out.println(rediscode);
            if(rediscode ==null){
                return CommonResult.failed("验证码过期，请重新获取");
            }else if (rediscode.equals(code+"") ) {
                Integer reg = userMemberService.reg(umsMember);
                if(reg>0){
                    return CommonResult.success("注册成功！");
                }else {
                    return CommonResult.success("注册失败！");
                }
            } else {
                return CommonResult.failed("验证码输入有误，请重新输入");

            }
        }
    }
    /**
     * 登录
     * @param umsMember
     * @return
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("logins")
    public CommonResult logins(UmsMember umsMember,Integer code){
        //密码需要客户端加密后传递
        String token = null;
        String rediscode= (String) redisService.get(umsMember.getPhone());
        if(rediscode ==null){
            return CommonResult.failed("验证码过期，请重新获取");
        }else if (rediscode.equals(code+"") ) {
            UmsMember umb=userMemberService.logins(umsMember);
            if(umb!=null) {
                token = jwtTokenUtil.generateToken(umb.getUsername());
                System.out.println(token);
                return CommonResult.success(token);
            }else {
                return CommonResult.success("登录失败");
            }
       }else{
                return CommonResult.failed("验证码输入有误，请重新输入");
           }
    }
    /**
     * 找回密码
     */
    //根据手机号和验证码通过验证

    //修改密码
    @ApiOperation(value = "修改密码")
    @PostMapping ("changePwd")
    public CommonResult changePwd(UmsMember umsMember, Integer code){
        Integer phone = userMemberService.findPhone(umsMember.getPhone());
        if(phone>0) {
            String rediscode = (String) redisService.get(umsMember.getPhone());
            if (rediscode == null) {
                return CommonResult.failed("验证码过期，请重新获取");
                } else if (rediscode.equals(code + "")) {//如果通过验证，则修改密码
                    int f = userMemberService.changePwd(umsMember);
                    if (f > 0) {//修改密码成功
                        return CommonResult.success("密码重置成功");
                    } else {//修改密码失败
                        return CommonResult.failed("密码重置失败");
                    }
                } else {//如果不通过验证，则返回验证失败信息
                    return CommonResult.failed("验证码错误");
                }
            }else{
                return CommonResult.failed("手机号码不存在");
            }
        }
    }





